<?php
class Zendvn_Model_Table_NewsItem extends Zend_Db_Table{
	protected $_name = "news_item";
	protected $_primary = "id"; 
	protected $_arrParam = array();
	
	public $_searchFields = array('name'=>'- Name field','code'=>'- Code field');
	
	protected $_listSQL;
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';				
			$data = array('status'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);	
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){						
						$where = 'id = ' . $val;
						$data = array('status'=>$status);
						$this->update($data, $where);						
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->newsItem($arrParam,$options,$this);
	}
	public function getItem($arrParam = null,$options = null){
		
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}		
		if($options['task'] == 'info'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from('news_item AS i')						
						->where('i.id = ?',$arrParam['id'],1)
						->joinLeft('news_category AS c', 'c.id = i.cat_id', array ('c.name AS cat_id' ))
						->joinLeft('users AS u1','u1.id = i.created_by', array ('u1.email AS created_by'))
						->joinLeft('users AS u2','u2.id = i.modified_by',array ('u2.email AS modified_by'));			
			$result = $db->fetchRow($select);		
		}	
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){
		if($options == null){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('news_item AS i')
						 ->joinLeft('news_category AS c', 'i.cat_id = c.id', array ('c.name AS cat_id' ))
						 ->joinLeft('users AS u1','u1.id = i.created_by', array ('u1.email AS created_by'))
						 ->joinLeft('users AS u2','u2.id = i.modified_by',array ('u2.email AS modified_by'));
			$arrFilter = $arrParam['arrFilter'];
			
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
			}
	
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where('i.status = ?',$arrFilter['status']);
			}
			
			if(!empty($arrFilter['cat_id']) && $arrFilter['cat_id'] != 1){
				$tblCate 		= new Zendvn_Model_Table_NewsCategory();
				$arrCid 		= $tblCate->getChildrenNode($arrFilter['cat_id'],array('node'=>true,'fields'=>array('id')));
				$select->where('i.cat_id IN (?)',$arrCid);
			}
			
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
			}
			
			$this->_listSQL = $select;
			$panigator = $arrParam['panigator'];	
			$select->limitPage($panigator['page'], $panigator['rowCount']);
			
			$result = $db->fetchAll($select);		 
		}
		
		if($options['task'] == 'list-news'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('news_item AS i')
						 ->where('i.cat_id = 2')
						 ->where('i.status = "active"')
						 ->order('i.id DESC');
			$select->limitPage(0, 3);
			$result = $db->fetchAll($select);	
		}
		if($options['task'] == 'list-archive'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('news_item AS i')
						 ->where('i.cat_id = 2')
						 ->where('i.id NOT IN (?)', $arrParam['arrId'])
						 ->where('i.status = "active"')
						 ->order('i.id DESC');
			$select->limitPage(0, 4);
			$result = $db->fetchAll($select);	
		}
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		if($options == null){
			$db = $this->getAdapter();
			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
			$select->columns('COUNT(i.id) AS total');	
			$result = $db->fetchOne($select);
		}
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		$this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null){
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		
		$row = $this->fetchNew();
		$row->name			= $arrParam['name'];			
		$row->picture		= $arrParam['picture'];
		$row->content		= $arrParam['content'];
		$row->summary		= $arrParam['summary'];	
		$row->html_descript	= $arrParam['html_descript'];
		$row->html_keyword	= $arrParam['html_keyword'];						
		$row->source_by		= $arrParam['source_by'];						
		$row->file_attach	= $arrParam['file_attach'];						
		$row->hits			= $arrParam['hits'];						
		$row->tags			= $arrParam['tags'];						
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		$row->created		= date('Y-m-d H:i:s');
		$row->created_by	= $idUser;
		$row->params		= Zend_Json::encode($arrParam['cparam']);
		$row->cat_id		= $arrParam['cat_id'];	
		$row->save();
			
	}
	
	public function adminEdit($arrParam = null){
		$info 				= new Zendvn_System_Info();
		$idUser 			= $info->getMemberInfo('id');
		
		$where  			= 'id = ' . (int)$arrParam['id'];
		$row 				= $this->fetchRow($where);
		$row->name			= $arrParam['name'];			
		$row->picture		= (!empty($arrParam['picture']))?$arrParam['picture']:$arrParam['current_picture'];
		$row->content		= $arrParam['content'];
		$row->summary		= $arrParam['summary'];	
		$row->html_descript	= $arrParam['html_descript'];	
		$row->html_keyword	= $arrParam['html_keyword'];						
		$row->source_by		= $arrParam['source_by'];
		$row->file_attach	= (!empty($arrParam['file_attach']))?$arrParam['file_attach']:$arrParam['current_file_attach'];							
		$row->hits			= $arrParam['hits'];						
		$row->tags			= $arrParam['tags'];						
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		$row->modified		= date('Y-m-d H:i:s');
		$row->modified_by	= $idUser;
		$row->params		= Zend_Json::encode($arrParam['cparam']);
		$row->cat_id		= $arrParam['cat_id'];
		$row->save();
	}
}